// pages/myself/shop/orderShop.js
var Zan = require('../../../dist/index');
var network = require('../../../utils/network_util.js');
var order = require('../../../template/orderTemp.js');
var api = require('../../../utils/api.js');
var start00 = 0;
var start01 = 0;
var app = getApp();

Page(Object.assign({}, Zan.TopTips, Zan.Tab, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: '未消费'
      }, {
        id: 1,
        title: '已消费'
      }],
      selectedId: 0,
      scroll: false
    },
    order00: [],
    order01: [],
    activeIndex: 0,
    radioCheckVal00: 0,
    radioCheckVal01: 0,
    endOrderId: '',
    storeId: app.globalData.storeId,
    nodata: {
      msg: '无相关数据',
      notype: 1,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network._post_page_json(this, api.getOrderList, start00, { status: 8, type: 1 }, success00);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   order00: [],
    // });
    // network._post_page_json(this, api.getOrderList, start00, { status: 8, type: 1 }, success00);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    /**
     * type: 0线上
     * type: 1线下
     */
    if (this.data.tab.selectedId == 0) {
      start00 = 0;
      this.setData({
        order00: [],
      });
      network._post_page_json(this, api.getOrderList, start00, { status: 8, type: 1 }, success00);
    } else if (this.data.tab.selectedId == 1) {
      start01 = 0;
      this.setData({
        order01: [],
      });
      network._post_page_json(this, api.getOrderList, start01, { status: 7, type: 1 }, success01);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.tab.selectedId == 0) {
      if (this.data.order00.length < this.data.radioCheckVal00) {
        start00 = start00 + 20;
        network._post_page_json(this, api.getOrderList, start00, { status: 8, type: 1 }, success00);
      }
    } else if (this.data.tab.selectedId == 1) {
      if (this.data.order01.length < this.data.radioCheckVal01) {
        start01 = start01 + 20;
        network._post_page_json(this, api.getOrderList, start01, { status: 7, type: 1 }, success01);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    });

    this.setData({
      activeIndex: selectedId
    })

    this.onPullDownRefresh();
  },

  //售后
  shouhouTk: function (e) {
    order.shouhouTk(0, e);
  },

  scan: function (e) {
    var orderId = e.currentTarget.dataset.id;
    this.setData({
      endOrderId: e.currentTarget.dataset.id
    })
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {    
        var scene = decodeURIComponent(res.path);
        var index = scene.indexOf('scene=s=')
        // app.globalData.storeId == scene.substring(2, scene.length);
        network._post_json(this, api.end, { orderId: orderId, storeId: scene.substring(index + 8, scene.length) }, endSuccess);
      }
    })
  },

  take: function (e) {
    var orderId = e.currentTarget.dataset.id;
    this.setData({
      endOrderId: e.currentTarget.dataset.id
    })
    network._post_json(this, api.end, { orderId: orderId, storeId: this.data.storeId }, endSuccess);
  },

  refresh: function () {
    this.onPullDownRefresh();
  }
}))

var success00 = function (that, res) {
  for (var j = 0; j < res.data.data.length; j++) {
    var product = res.data.data[j].product;
    for (var i = 0; i < product.length; i++) {
      if (product[i].productSpec) {
        product[i].price = product[i].productSpec.price;
        product[i].specId = product[i].productSpec.id;
        product[i].specName = product[i].productSpec.name;
      }
    }
    res.data.data[j].product = product;
  }
  that.setData({
    order00: that.data.order00.concat(res.data.data),
    recordsTotal00: res.data.recordsTotal00
  })
}

var success01 = function (that, res) {
  for (var j = 0; j < res.data.data.length; j++) {
    var product = res.data.data[j].product;
    for (var i = 0; i < product.length; i++) {
      if (product[i].productSpec) {
        product[i].price = product[i].productSpec.price;
        product[i].specId = product[i].productSpec.id;
        product[i].specName = product[i].productSpec.name;
      }
    }
    res.data.data[j].product = product;
  }
  that.setData({
    order01: that.data.order01.concat(res.data.data),
    recordsTotal01: res.data.recordsTotal01
  })
}

var endSuccess = function (that, res) {
  if (that.data.tab.selectedId == 0) {
    that.setData({
      order00: Array.prototype.remove(that.data.order00, that.data.endOrderId)
    })
  }
  that.showZanToast('扫码成功');
}

Array.prototype.remove = function (order, id) {
  for (var i = 0; i < order.length; i++) {
    if (order[i].id == id) {
      order.splice(i, 1);
    }
  }
  return order;
}